package BinaryTree;

public class TreeOperations {
    public static int count;//可以用到所有方法中
    public static void countNodeSize(TreeNode root) {
        if(root==null) {
            return;
        }

        count++;
        countNodeSize(root.right);
        countNodeSize(root.left);
    }

    public static int countNodeSize2(TreeNode root) {
        if (root==null) {
            return 0;
        }

        int subtreeLeftSize=countNodeSize2(root.left);
        int subtreeRightSize=countNodeSize2(root.right);

        return 1+subtreeLeftSize+subtreeRightSize;//根+左子树结点个数+右子树结点个数
    }

    public static int leafcount;//每次调用记得初始化leafcount！！！！
    public static void countLeafNodeSize(TreeNode root) {
        //求树中叶子结点的个数
        if(root==null) {
            return;
        }

        if(root.left==null && root.right==null) {
            leafcount++;
        }

        countLeafNodeSize(root.left);
        countLeafNodeSize(root.right);

    }

    public static int countLeafNodeSize2(TreeNode root) {

        if(root==null) {
            return 0;
        }

        if(root.right==null && root.left==null) {
            return 1;
        }
        int leftSize=countNodeSize2(root.left);
        int rightSize=countNodeSize2(root.right);

        return leftSize+rightSize;//左子树叶子结点+右子树叶子结点
    }

    public static void main(String[] args) {
        TreeNode root=BuildTree构建树.buildTree2();
        countLeafNodeSize2(root);
        System.out.println("2222");


//        count=0;
//        countNodeSize(root);
//        System.out.println("结点个数为："+count);
//
//
//        count=0;
//        countNodeSize(root);
//        System.out.println("结点个数为："+count);
//
//        System.out.println("结点个数为："+countNodeSize2(root));
//
//        leafcount=0;
//        countLeafNodeSize(root);
//        System.out.println("叶子结点个数为:"+leafcount);
//
//        leafcount=0;
//        countLeafNodeSize(root);
//        System.out.println("叶子结点个数为:"+leafcount);
    }
}
